package com.budwk.app.sqfwV2.service.patrolAndVisit;

import cn.hutool.json.JSONUtil;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sqfwDoc.bo.ExcelCreateConfig;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.AbstractCustomerPdfCreateField;
import com.budwk.app.sqfwDoc.codeGeneratorV2.config.ExcelGeneratorConfig;
import com.budwk.app.sqfwDoc.codeGeneratorV2.excelService.CompleteFlowExcelService;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CompleteFlowExcelParamForController;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.ExcelGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.ExcelRowDataParam;
import com.budwk.app.sqfwV2.dto.PropagandaScreeningDTO;
import com.budwk.app.sqfwV2.enums.PatrolAndVisitEnum;
import com.budwk.app.sqfwV2.generator.patrolAndVisit.PatrolAndVisitExcelForAdminGenerator;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import com.google.common.collect.Lists;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

@IocBean
public class PatrolAndVisitExcelForAdminService extends CompleteFlowExcelService {


    @Override
    protected void customerPopularExcelGenerateParam(ExcelGenerateParam excelGenerateParam) {

    }

    @Override
    public List<ExcelGeneratorConfig> getExcelGeneratorConfig(List<ExcelCreateConfig> excelCreateConfigList) {
        List<ExcelGeneratorConfig> excelGeneratorConfigs = new ArrayList<>();
        ExcelGeneratorConfig generatorConfig = new ExcelGeneratorConfig();
        String code = excelCreateConfigList.get(0).getFileIdentity();
        generatorConfig.setExcelGenerator(new PatrolAndVisitExcelForAdminGenerator(PatrolAndVisitEnum.getPdfTemplat(Integer.parseInt(code))));
        generatorConfig.setExcelCreateConfig(excelCreateConfigList.get(0));
        excelGeneratorConfigs.add(generatorConfig);
        return excelGeneratorConfigs;
    }

    @Override
    public AbstractCustomerPdfCreateField getAbstractCustomerField(String jsonStr) {
        return JSONUtil.toBean(jsonStr, PropagandaScreeningDTO.class);
    }

    @Override
    protected List<ExcelRowDataParam> getAdminExcelRowData(CompleteFlowExcelParamForController paramForController) {
        SqfwFormsummaryNewService sqfwFormsummaryNewService = NutzIocUtils.getBean(SqfwFormsummaryNewService.class);
        List<Sqfw_formSummary_new> formSummaryNewList = sqfwFormsummaryNewService.query(Cnd.where(Sqfw_formSummary_new::getId, "IN", paramForController.getFormSummaryIdList()));
        List<Sqfw_formSummary_new> sqfwFormSummaryNewList = Lists.newArrayList();
        Map<String, List<Sqfw_formSummary_new>> sqfwNewMap
                = formSummaryNewList.stream().collect(Collectors.groupingBy(Sqfw_formSummary_new::getCommunity, HashMap::new, Collectors.toList()));
        sqfwNewMap.forEach((key, value) -> {
            Sqfw_formSummary_new sqfwFormSummaryNew = new Sqfw_formSummary_new();
            sqfwFormSummaryNew.setCommunity(key);
            AtomicReference<Integer> publicityRecord = new AtomicReference<>(0);
            AtomicReference<Integer> searchRecord = new AtomicReference<>(0);
            AtomicReference<Integer> peaceAndCare = new AtomicReference<>(0);
            value.forEach(item -> {
                Sqfw_formSummary_new sqfw_formSummary_new = item;
                sqfwFormSummaryNew.setProvince(sqfw_formSummary_new.getProvince());
                sqfwFormSummaryNew.setDistrict(sqfw_formSummary_new.getDistrict());
                sqfwFormSummaryNew.setCity(sqfw_formSummary_new.getCity());
                sqfwFormSummaryNew.setCounty(sqfw_formSummary_new.getCounty());
                PropagandaScreeningDTO propagandaScreeningDTO = JSONUtil.toBean(sqfw_formSummary_new.getParamData(), PropagandaScreeningDTO.class);
                if (PatrolAndVisitEnum.A.getName().equals(propagandaScreeningDTO.getType())) {
                    publicityRecord.set(publicityRecord.get() + Arrays.asList(propagandaScreeningDTO.getTheme().split(",")).size());
                } else if (PatrolAndVisitEnum.B.getName().equals(propagandaScreeningDTO.getType())) {
                    searchRecord.set(searchRecord.get() + Arrays.asList(propagandaScreeningDTO.getTheme().split(",")).size());
                } else if (PatrolAndVisitEnum.C.getName().equals(propagandaScreeningDTO.getType())) {
                    peaceAndCare.set(peaceAndCare.get() + Arrays.asList(propagandaScreeningDTO.getTheme().split(",")).size());
                }
            });
            Map<String, Integer> maps = new HashMap<String, Integer>();
            maps.put("publicityRecord", publicityRecord.get());
            maps.put("searchRecord", searchRecord.get());
            maps.put("peaceAndCare", peaceAndCare.get());
            sqfwFormSummaryNew.setUnitName(JSONUtil.toJsonStr(maps));
            sqfwFormSummaryNewList.add(sqfwFormSummaryNew);
        });
        return transformFormSummaryToExcelRowData(sqfwFormSummaryNewList);
    }

    /**
     * 在创建生成参数的时候允许自定义填充数据
     *
     * @param formSummaryNew 表单数据
     */
    public void customerPopularExcelRowDataParam(ExcelRowDataParam rowDataParam, Sqfw_formSummary_new formSummaryNew) {
        Map<String, Object> customerParamMap = new HashMap<>();
        customerParamMap.put("province", formSummaryNew.getProvince());
        customerParamMap.put("city", formSummaryNew.getCity());
        customerParamMap.put("district", formSummaryNew.getDistrict());
        customerParamMap.put("community", formSummaryNew.getCommunity());
        customerParamMap.put("county", formSummaryNew.getCounty());
        Map<String, String> maps = JSONUtil.toBean(formSummaryNew.getUnitName(), Map.class);
        customerParamMap.put("publicityRecord", maps.get("publicityRecord"));
        customerParamMap.put("searchRecord", maps.get("searchRecord"));
        customerParamMap.put("peaceAndCare", maps.get("peaceAndCare"));
        rowDataParam.setCustomerParamMap(customerParamMap);
    }
}